# -*- coding: utf-8 -*-
# 坦克大战
import pygame
import sys
import traceback
import wall
import myTank
import enemyTank
import food
import time
    

def main():
    pygame.init()
    # 初始化音频混合器的函数
    pygame.mixer.init()
    # 游戏窗口大小
    resolution = 630, 630
    screen = pygame.display.set_mode(resolution)
    pygame.display.set_caption("Tank War")
    
    # 加载图片,音乐,音效.
    background_image = pygame.image.load(r"./image/background.png")
    
    bang_sound = pygame.mixer.Sound(r"./music/bang.wav")
    add_sound = pygame.mixer.Sound(r"./music/add.wav")
    bang_sound.set_volume(1)
    fire_sound = pygame.mixer.Sound(r"./music/Gunfire.wav")
    start_sound = pygame.mixer.Sound(r"./music/start.wav")
    start_sound.play()
    
    # 定义精灵组:坦克，我方坦克，敌方坦克，敌方子弹
    allTankGroup = pygame.sprite.Group()       # 所有坦克
    mytankGroup = pygame.sprite.Group()        # 我方坦克
    allEnemyGroup = pygame.sprite.Group()      # 所有敌方坦克
    redEnemyGroup = pygame.sprite.Group()      # 红色敌方坦克
    greenEnemyGroup = pygame.sprite.Group()    # 绿色敌方坦克
    otherEnemyGroup = pygame.sprite.Group()    # 其它敌方坦克
    enemyBulletGroup = pygame.sprite.Group()   # 敌方坦克子弹

    # 游戏分数
    score = 0


    # 创建地图 
    bgMap = wall.Map()
    # 创建食物/道具 但不显示
    prop = food.Food()
    # 创建我方坦克
    myTank_T1 = myTank.MyTank(1)
    allTankGroup.add(myTank_T1)
    mytankGroup.add(myTank_T1)


    myTank_T2 = myTank.MyTank(2)
    allTankGroup.add(myTank_T2)
    mytankGroup.add(myTank_T2)
    print(f"----------------------{bgMap.home.isNotDestroy}")
    # 创建敌方 坦克
    for i in range(1, 4):
            enemy = enemyTank.EnemyTank(i)
            allTankGroup.add(enemy)
            allEnemyGroup.add(enemy)
            if enemy.isred == True:
                redEnemyGroup.add(enemy)
                continue
            if enemy.kind == 3:
                greenEnemyGroup.add(enemy)
                continue
            otherEnemyGroup.add(enemy)
    # 敌军坦克出现动画
    appearance_image = pygame.image.load(r"./image/appear.png").convert_alpha()
    appearance = []
    # 第一个参数为位置，第二个参数为大小
    appearance.append(appearance_image.subsurface(( 0, 0), (48, 48)))
    appearance.append(appearance_image.subsurface((48, 0), (48, 48)))
    appearance.append(appearance_image.subsurface((96, 0), (48, 48)))

    # 自定义事件(定时器事件)
    # 创建敌方坦克延迟200
    DELAYEVENT = pygame.constants.USEREVENT
    pygame.time.set_timer(DELAYEVENT, 200)
    # 创建 敌方 子弹延迟1000
    ENEMYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 1
    pygame.time.set_timer(ENEMYBULLETNOTCOOLINGEVENT, 1000)
    # 创建 我方 子弹延迟200
    MYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 2
    pygame.time.set_timer(MYBULLETNOTCOOLINGEVENT, 200)
    # 敌方坦克 静止8000
    NOTMOVEEVENT = pygame.constants.USEREVENT + 3
    pygame.time.set_timer(NOTMOVEEVENT, 8000)
    
    
    delay = 100
    moving = 0
    movdir = 0
    moving2 = 0
    movdir2 = 0
    # 敌方坦克数量
    enemyNumber = 3
    # 敌方坦克是否可以移动
    enemyCouldMove = True
    switch_R1_R2_image = True
    # 堡垒是否存在:True：存在，游戏继续，False：游戏结束
    homeSurvive = True
    running_T1 = True
    running_T2 = True
    clock = pygame.time.Clock()
    while True:
        # 获取所有事件，并逐个处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            
            # 我方子弹冷却事件：将该参数设置为True，表示可以发射子弹(控制子弹发生速度)
            if event.type == MYBULLETNOTCOOLINGEVENT:
                myTank_T1.bulletNotCooling = True
                
            # 敌方子弹冷却事件
            if event.type == ENEMYBULLETNOTCOOLINGEVENT:
                for each in allEnemyGroup:
                    each.bulletNotCooling = True
            
            # 敌方坦克静止事件
            if event.type == NOTMOVEEVENT:
                enemyCouldMove = True
            
            # 创建敌方坦克延迟据当前敌方坦克数量，创建一个新的敌方坦克，并将其添加到相应的精灵组中
            if event.type == DELAYEVENT:
                if score < 100:
                    if enemyNumber < 4:
                        enemy = enemyTank.EnemyTank()
                        # 判断新生成的坦克是否与其它坦克重叠
                        if pygame.sprite.spritecollide(enemy, allTankGroup, False, None):
                            break
                        allEnemyGroup.add(enemy)
                        allTankGroup.add(enemy)
                        enemyNumber += 1
                        if enemy.isred == True:
                            redEnemyGroup.add(enemy)
                        elif enemy.kind == 3:
                            greenEnemyGroup.add(enemy)
                        else:
                            otherEnemyGroup.add(enemy)
                elif 100 < score < 250:
                    if enemyNumber < 6:
                        enemy = enemyTank.EnemyTank()
                        # 判断新生成的坦克是否与其它坦克重叠
                        if pygame.sprite.spritecollide(enemy, allTankGroup, False, None):
                            break
                        allEnemyGroup.add(enemy)
                        allTankGroup.add(enemy)
                        enemyNumber += 1
                        if enemy.isred == True:
                            redEnemyGroup.add(enemy)
                        elif enemy.kind == 3:
                            greenEnemyGroup.add(enemy)
                        else:
                            otherEnemyGroup.add(enemy)
                elif 250 < score < 600:
                    if enemyNumber < 10:
                        enemy = enemyTank.EnemyTank()
                        # 判断新生成的坦克是否与其它坦克重叠
                        if pygame.sprite.spritecollide(enemy, allTankGroup, False, None):
                            break
                        allEnemyGroup.add(enemy)
                        allTankGroup.add(enemy)
                        enemyNumber += 1
                        if enemy.isred == True:
                            redEnemyGroup.add(enemy)
                        elif enemy.kind == 3:
                            greenEnemyGroup.add(enemy)
                        else:
                            otherEnemyGroup.add(enemy)
                elif 600 < score < 1000:
                    if enemyNumber < 15:
                        enemy = enemyTank.EnemyTank()
                        # 判断新生成的坦克是否与其它坦克重叠
                        if pygame.sprite.spritecollide(enemy, allTankGroup, False, None):
                            break
                        allEnemyGroup.add(enemy)
                        allTankGroup.add(enemy)
                        enemyNumber += 1
                        if enemy.isred == True:
                            redEnemyGroup.add(enemy)
                        elif enemy.kind == 3:
                            greenEnemyGroup.add(enemy)
                        else:
                            otherEnemyGroup.add(enemy)
                else:
                    if enemyNumber < 25:
                        enemy = enemyTank.EnemyTank()
                        # 判断新生成的坦克是否与其它坦克重叠
                        if pygame.sprite.spritecollide(enemy, allTankGroup, False, None):
                            break
                        allEnemyGroup.add(enemy)
                        allTankGroup.add(enemy)
                        enemyNumber += 1
                        if enemy.isred == True:
                            redEnemyGroup.add(enemy)
                        elif enemy.kind == 3:
                            greenEnemyGroup.add(enemy)
                        else:
                            otherEnemyGroup.add(enemy)



            # 按键事件
            if event.type == pygame.KEYDOWN:
                # Ctrl + C退出游戏
                if event.key == pygame.K_c and pygame.KMOD_CTRL:
                    pygame.quit()
                    sys.exit()
                if event.key == pygame.K_e:  # 按下E：我方坦克升级
                    print("我方坦克升级")
                    myTank_T1.levelUp()
                    myTank_T2.levelUp()
                if event.key == pygame.K_q:  # 按下q：我方坦克降级
                    print("我方坦克降级")
                    myTank_T1.levelDown()
                    myTank_T2.levelDown()
                if event.key == pygame.K_3:    # 按下3：我方坦克升至最高级
                    print("我方坦克升级至最高等级")
                    myTank_T1.levelUp()
                    myTank_T1.levelUp()
                    myTank_T1.level = 3
                    myTank_T2.levelUp()
                    myTank_T2.levelUp()
                    myTank_T2.level = 3
                if event.key == pygame.K_2:    # 按下2：切换我方坦克速度
                    print("我方坦克速度切换")
                    if myTank_T1.speed == 3:
                        myTank_T1.speed = 24
                    else:
                        myTank_T1.speed = 3
                    if myTank_T2.speed == 3:
                        myTank_T2.speed = 24
                    else:
                        myTank_T2.speed = 3
                if event.key == pygame.K_1:    # 按下1：在地图中放置一些砖块
                    print("放置砖块")
                    for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]:
                        bgMap.brick = wall.Brick()
                        bgMap.brick.rect.left, bgMap.brick.rect.top = 3 + x * 24, 3 + y * 24
                        bgMap.brickGroup.add(bgMap.brick)                
                if event.key == pygame.K_4:   # 按下4：在地图中放置一些铁块
                    print("放置铁块")
                    for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]:
                        bgMap.iron = wall.Iron()
                        bgMap.iron.rect.left, bgMap.iron.rect.top = 3 + x * 24, 3 + y * 24
                        bgMap.ironGroup.add(bgMap.iron)                
                


        # 检查用户的键盘操作
        key_pressed = pygame.key.get_pressed()
        # 玩家一的移动操作
        if moving and bgMap.home.isNotDestroy == True:
            moving -= 1
            if movdir == 0:
                allTankGroup.remove(myTank_T1)
                if myTank_T1.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
                    moving += 1
                allTankGroup.add(myTank_T1)
                running_T1 = True
            if movdir == 1:
                allTankGroup.remove(myTank_T1)
                if myTank_T1.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
                    moving += 1
                allTankGroup.add(myTank_T1)
                running_T1 = True
            if movdir == 2:
                allTankGroup.remove(myTank_T1)
                if myTank_T1.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
                    moving += 1
                allTankGroup.add(myTank_T1)
                running_T1 = True
            if movdir == 3:
                allTankGroup.remove(myTank_T1)
                if myTank_T1.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
                    moving += 1
                allTankGroup.add(myTank_T1)
                running_T1 = True
                
        if not moving and bgMap.home.isNotDestroy == True:
            if key_pressed[pygame.K_w]:
                moving = 7
                movdir = 0
                running_T1 = True
                allTankGroup.remove(myTank_T1)
                if myTank_T1.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
                    moving = 0
                allTankGroup.add(myTank_T1)
            elif key_pressed[pygame.K_s]:
                moving = 7
                movdir = 1
                running_T1 = True
                allTankGroup.remove(myTank_T1)
                if myTank_T1.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
                    moving = 0
                allTankGroup.add(myTank_T1)
            elif key_pressed[pygame.K_a]:
                moving = 7
                movdir = 2
                running_T1 = True
                allTankGroup.remove(myTank_T1)
                if myTank_T1.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
                    moving = 0
                allTankGroup.add(myTank_T1)
            elif key_pressed[pygame.K_d]:
                moving = 7
                movdir = 3
                running_T1 = True
                allTankGroup.remove(myTank_T1)
                if myTank_T1.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
                    moving = 0
                allTankGroup.add(myTank_T1)
        if key_pressed[pygame.K_g]:
            if not myTank_T1.bullet.life and myTank_T1.bulletNotCooling:
                fire_sound.play()
                myTank_T1.shoot()
                myTank_T1.bulletNotCooling = False
                
        # 玩家二的移动操作
        if moving2 and bgMap.home.isNotDestroy == True:
            moving2 -= 1
            if movdir2 == 0:
                allTankGroup.remove(myTank_T2)
                myTank_T2.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
                allTankGroup.add(myTank_T2)
                running_T2 = True
            if movdir2 == 1:
                allTankGroup.remove(myTank_T2)
                myTank_T2.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
                allTankGroup.add(myTank_T2)
                running_T2 = True
            if movdir2 == 2:
                allTankGroup.remove(myTank_T2)
                myTank_T2.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
                allTankGroup.add(myTank_T2)
                running_T2 = True
            if movdir2 == 3:
                allTankGroup.remove(myTank_T2)
                myTank_T2.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
                allTankGroup.add(myTank_T2)
                running_T2 = True
                
        if not moving2 and bgMap.home.isNotDestroy == True:
            if key_pressed[pygame.K_UP]:
                allTankGroup.remove(myTank_T2)
                myTank_T2.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
                allTankGroup.add(myTank_T2)
                moving2 = 7
                movdir2 = 0
                running_T2 = True
            elif key_pressed[pygame.K_DOWN]:
                allTankGroup.remove(myTank_T2)
                myTank_T2.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
                allTankGroup.add(myTank_T2)
                moving2 = 7
                movdir2 = 1
                running_T2 = True
            elif key_pressed[pygame.K_LEFT]:
                allTankGroup.remove(myTank_T2)
                myTank_T2.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
                allTankGroup.add(myTank_T2)
                moving2 = 7
                movdir2 = 2
                running_T2 = True
            elif key_pressed[pygame.K_RIGHT]:
                allTankGroup.remove(myTank_T2)
                myTank_T2.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
                allTankGroup.add(myTank_T2)
                moving2 = 7
                movdir2 = 3
                running_T2 = True
        if key_pressed[pygame.K_j]:
            if not myTank_T2.bullet.life:
                fire_sound.play()
                myTank_T2.shoot()
        
        # 画背景
        screen.blit(background_image, (0, 0))
        # 画砖块
        for each in bgMap.brickGroup:
            screen.blit(each.image, each.rect)        
        # 花石头
        for each in bgMap.ironGroup:
            screen.blit(each.image, each.rect)        
        # 画home
        if bgMap.home.life == 2:
            screen.blit(bgMap.home.image,(bgMap.home.rect.x,bgMap.home.rect.y))
        elif bgMap.home.life == 1:
            screen.blit(bgMap.home.image2,(bgMap.home.rect.x,bgMap.home.rect.y))
        else:
            text = pygame.font.Font('./font/LycheeSoda.ttf', 90).render('GAME  OVER', True, (215, 55, 74))
            screen.blit(text, (100, 200))
            screen.blit(bgMap.home.image3,(bgMap.home.rect.x,bgMap.home.rect.y))
            text2 = pygame.font.Font('./font/LycheeSoda.ttf', 90).render(f'SCORE: {score}', True, (255, 255, 255))
            screen.blit(text2, (180, 300))




        # 画我方坦克1
        if not (delay % 5):
            switch_R1_R2_image = not switch_R1_R2_image
        if myTank_T1.live > 0:
            if switch_R1_R2_image and running_T1:
                screen.blit(myTank_T1.tank_R0, (myTank_T1.rect.left, myTank_T1.rect.top))
                running_T1 = False
            else:
                screen.blit(myTank_T1.tank_R1, (myTank_T1.rect.left, myTank_T1.rect.top))
        else:
            myTank_T1.rect.x = -1000
            myTank_T1.rect.y = 1000
            myTank_T1.life = False




        # 画我方坦克2
        if not (delay % 5):
            switch_R1_R2_image = not switch_R1_R2_image
        if myTank_T2.live > 0:
            if switch_R1_R2_image and running_T2:
                screen.blit(myTank_T2.tank_R0, (myTank_T2.rect.left, myTank_T2.rect.top))
                running_T2 = False
            else:
                screen.blit(myTank_T2.tank_R1, (myTank_T2.rect.left, myTank_T2.rect.top))
        else:
            myTank_T2.rect.x = -1000
            myTank_T2.rect.y = 1000
            myTank_T2.life = False

        if myTank_T1.life == False and myTank_T2.life == False:
            print("游戏结束")
            text = pygame.font.Font('./font/LycheeSoda.ttf', 90).render('GAME  OVER', True, (215, 55, 74))
            screen.blit(text, (100, 200))
            screen.blit(bgMap.home.image3, (bgMap.home.rect.x, bgMap.home.rect.y))





        # 画敌方坦克
        for each in allEnemyGroup:
            # 判断5毛钱特效是否播放            
            if each.flash:
                #　判断画左动作还是右动作
                if switch_R1_R2_image:
                    screen.blit(each.tank_R0, (each.rect.left, each.rect.top))
                    if enemyCouldMove:
                        allTankGroup.remove(each)
                        each.move(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
                        allTankGroup.add(each)
                else:
                    screen.blit(each.tank_R1, (each.rect.left, each.rect.top))
                    if enemyCouldMove:
                        allTankGroup.remove(each)
                        each.move(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
                        allTankGroup.add(each)                    
            else:
                # 播放5毛钱特效
                if each.times > 0:
                    each.times -= 1
                    if each.times <= 10:
                        screen.blit(appearance[2], (3 + each.x * 12 * 24, 3))
                    elif each.times <= 20:
                        screen.blit(appearance[1], (3 + each.x * 12 * 24, 3))
                    elif each.times <= 30:
                        screen.blit(appearance[0], (3 + each.x * 12 * 24, 3))
                    elif each.times <= 40:
                        screen.blit(appearance[2], (3 + each.x * 12 * 24, 3))
                    elif each.times <= 50:
                        screen.blit(appearance[1], (3 + each.x * 12 * 24, 3))
                    elif each.times <= 60:
                        screen.blit(appearance[0], (3 + each.x * 12 * 24, 3))
                    elif each.times <= 70:
                        screen.blit(appearance[2], (3 + each.x * 12 * 24, 3))
                    elif each.times <= 80:
                        screen.blit(appearance[1], (3 + each.x * 12 * 24, 3))
                    elif each.times <= 90:
                        screen.blit(appearance[0], (3 + each.x * 12 * 24, 3))
                if each.times == 0:
                    each.flash = True


        """
        检测我方堡垒
        """
        if bgMap.home.isNotDestroy:

            #敌军子弹是否击中
            for each in enemyBulletGroup:
                if each.life:
                    
                    if pygame.sprite.collide_rect(bgMap.home,each):
                        bang_sound.play()
                        print("堡垒受到敌军攻击")
                        each.life = False
                        bgMap.home.life -= 1
                        if bgMap.home.life <= 0:
                            bgMap.home.isNotDestroy = False
            if myTank_T1.bullet.life:
                if pygame.sprite.collide_rect(bgMap.home, myTank_T1.bullet):
                    print(f"home{bgMap.home.rect},mytank_T1.bullet{myTank_T1.bullet.rect}")
                    print("笨蛋，堡垒受到玩家1攻击")
                    bang_sound.play()
                    myTank_T1.bullet.life = False
                    bgMap.home.life -= 1
                    if bgMap.home.life <= 0:
                        bgMap.home.isNotDestroy = False
            if myTank_T2.bullet.life:
                if pygame.sprite.collide_rect(bgMap.home, myTank_T2.bullet):
                    print("笨蛋，堡垒受到玩家2攻击")
                    bang_sound.play()
                    myTank_T2.bullet.life = False
                    bgMap.home.life -= 1
                    if bgMap.home.life <= 0:
                        bgMap.home.isNotDestroy = False

      
                 
        """
        我方子弹1攻击效果
        """
        if myTank_T1.bullet.life and bgMap.home.isNotDestroy == True:
            myTank_T1.bullet.move()    
            screen.blit(myTank_T1.bullet.bullet, myTank_T1.bullet.rect)
            # 子弹与我方子弹碰撞
            if pygame.sprite.collide_rect(myTank_T1.bullet,myTank_T2.bullet):
                print("我方子弹相互碰撞")
                myTank_T1.bullet.life = False
                myTank_T2.bullet.life = False
            # 子弹击伤友军
            if pygame.sprite.collide_rect(myTank_T1.bullet,myTank_T2):
                print("笨蛋，击中玩家二")
                bang_sound.play()
                myTank_T1.bullet.life = False
                myTank_T2.live -= 1
                myTank_T2.rect.left, myTank_T2.rect.top = 3 + 16 * 24, 3 + 24 * 24
                moving2 = 0  # 重置移动控制参数
                add_sound.play()
                for i in range(myTank_T2.level + 1):
                    myTank_T2.levelDown()
            # 子弹 碰撞 子弹
            for each in enemyBulletGroup:

                if each.life:
                    if pygame.sprite.collide_rect(myTank_T1.bullet, each):
                        print("玩家一：我方子弹与敌方子弹碰撞")
                        myTank_T1.bullet.life = False
                        each.life = False
                        pygame.sprite.spritecollide(myTank_T1.bullet, enemyBulletGroup, True, None)
            # 子弹 碰撞 敌方坦克
            if pygame.sprite.spritecollide(myTank_T1.bullet, redEnemyGroup, True, None):
                print("玩家一：击中红色敌方坦克")
                score += 10
                print(f"此时的游戏分数为：{score}")
                prop.change()
                bang_sound.play()
                enemyNumber -= 1
                myTank_T1.bullet.life = False
            elif pygame.sprite.spritecollide(myTank_T1.bullet,greenEnemyGroup, False, None):
                print("玩家一：击中绿色敌方坦克")
                for each in greenEnemyGroup:
                    if pygame.sprite.collide_rect(myTank_T1.bullet, each):
                        if each.life == 1:
                            pygame.sprite.spritecollide(myTank_T1.bullet,greenEnemyGroup, True, None)
                            bang_sound.play()
                            enemyNumber -= 1
                            score += 20
                            print(f"此时的游戏分数为：{score}")
                        elif each.life == 2:
                            each.life -= 1
                            each.tank = each.enemy_3_0
                        elif each.life == 3:
                            each.life -= 1
                            each.tank = each.enemy_3_2
                myTank_T1.bullet.life = False
            elif pygame.sprite.spritecollide(myTank_T1.bullet, otherEnemyGroup, True, None):
                print("玩家一：击中其它坦克")
                bang_sound.play()
                enemyNumber -= 1
                myTank_T1.bullet.life = False
                score += 5
                print(f'此时的游戏分数为：{score}')
            # 子弹 碰撞 brickGroup
            if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.brickGroup, True, None):
                print("玩家一：子弹击中砖块")
                myTank_T1.bullet.life = False
                myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24



            # 增强子弹 碰撞 brickGroup
            if myTank_T1.bullet.strong:
                if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.ironGroup, True, None):
                    myTank_T1.bullet.life = False
                    myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
            else:    
                if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.ironGroup, False, None):
                    print("玩家一：子弹击中铁块")
                    myTank_T1.bullet.life = False
                    myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
        
        """
        我方子弹2攻击效果
        """
        if myTank_T2.bullet.life and bgMap.home.isNotDestroy == True:
            myTank_T2.bullet.move()    
            screen.blit(myTank_T2.bullet.bullet, myTank_T2.bullet.rect)
            if pygame.sprite.collide_rect(myTank_T2.bullet,myTank_T1):
                print("笨蛋，击中玩家一")
                bang_sound.play()
                myTank_T2.bullet.life = False
                myTank_T1.live -= 1
                myTank_T1.rect.left, myTank_T1.rect.top = 3 + 8 * 24, 3 + 24 * 24
                moving = 0  # 重置移动控制参数
                add_sound.play()
                for i in range(myTank_T1.level + 1):
                    myTank_T1.levelDown()
            # 子弹 碰撞 子弹
            for each in enemyBulletGroup:
                if each.life:
                    if pygame.sprite.collide_rect(myTank_T2.bullet, each):
                        print("玩家二：我方子弹与敌方子弹碰撞")
                        myTank_T2.bullet.life = False
                        each.life = False
                        pygame.sprite.spritecollide(myTank_T2.bullet, enemyBulletGroup, True, None)

            # 子弹 碰撞 敌方坦克
            if pygame.sprite.spritecollide(myTank_T2.bullet, redEnemyGroup, True, None):
                print("玩家二：击中红色敌方坦克")
                prop.change()
                bang_sound.play()
                enemyNumber -= 1
                myTank_T2.bullet.life = False
                score += 10
                print(f"此时的游戏分数为：{score}")
            elif pygame.sprite.spritecollide(myTank_T2.bullet, greenEnemyGroup, False, None):
                print("玩家二：击中绿色敌方坦克")
                for each in greenEnemyGroup:
                    if pygame.sprite.collide_rect(myTank_T2.bullet, each):
                        if each.life == 1:
                            pygame.sprite.spritecollide(myTank_T2.bullet, greenEnemyGroup, True, None)
                            bang_sound.play()
                            enemyNumber -= 1
                            score += 20
                            print(f"此时的游戏分数为：{score}")
                        elif each.life == 2:
                            each.life -= 1
                            each.tank = each.enemy_3_0
                        elif each.life == 3:
                            each.life -= 1
                            each.tank = each.enemy_3_2
                myTank_T2.bullet.life = False
            elif pygame.sprite.spritecollide(myTank_T2.bullet, otherEnemyGroup, True, None):
                print("玩家二：击中其它坦克")
                bang_sound.play()
                enemyNumber -= 1
                myTank_T2.bullet.life = False
                score += 5
                print(f"此时的游戏分数为：{score}")

            # 子弹 碰撞 brickGroup
            if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.brickGroup, True, None):
                print("玩家二：击中砖块")
                myTank_T2.bullet.life = False
                myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
            # 子弹 碰撞 brickGroup
            if myTank_T2.bullet.strong:   # 加强子弹可击穿铁块
                if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.ironGroup, True, None):
                    myTank_T2.bullet.life = False
                    myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
            else:    
                if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.ironGroup, False, None):
                    print("玩家二：击中铁块")
                    myTank_T2.bullet.life = False
                    myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
        

        # 绘制敌人子弹
        for each in allEnemyGroup:
            # 如果子弹没有生命，则赋予子弹生命
            if not each.bullet.life and each.bulletNotCooling and enemyCouldMove:
                enemyBulletGroup.remove(each.bullet)
                each.shoot()
                enemyBulletGroup.add(each.bullet)
                each.bulletNotCooling = False
            # 如果5毛钱特效播放完毕 并且 子弹存活 则绘制敌方子弹
            if each.flash:
                if each.bullet.life:
                    # 如果敌人可以移动
                    if enemyCouldMove:
                        each.bullet.move()
                    screen.blit(each.bullet.bullet, each.bullet.rect)

                    # 子弹 碰撞 我方坦克
                    if pygame.sprite.collide_rect(each.bullet, myTank_T1):
                        myTank_T1.live -= 1
                        print(f"玩家1被敌方坦克击中,此时的生命值{myTank_T1.live}")
                        bang_sound.play()
                        myTank_T1.rect.left, myTank_T1.rect.top = 3 + 8 * 24, 3 + 24 * 24 
                        each.bullet.life = False
                        moving = 0  # 重置移动控制参数
                        add_sound.play()
                        for i in range(myTank_T1.level+1):
                            myTank_T1.levelDown()
                    if pygame.sprite.collide_rect(each.bullet, myTank_T2):
                        print(f"玩家2被敌方坦克击中,此时的生命值{myTank_T2.live}")
                        myTank_T2.live -= 1
                        bang_sound.play()
                        myTank_T2.rect.left, myTank_T2.rect.top = 3 + 16 * 24, 3 + 24 * 24 
                        each.bullet.life = False
                        moving2 = 0  # 重置移动控制参数
                        add_sound.play()
                        for i in range(myTank_T2.level + 1):
                            myTank_T2.levelDown()

                    # 子弹 碰撞 brickGroup
                    if pygame.sprite.spritecollide(each.bullet, bgMap.brickGroup, True, None):
                        each.bullet.life = False
                    # 子弹 碰撞 ironGroup
                    if each.bullet.strong:
                        if pygame.sprite.spritecollide(each.bullet, bgMap.ironGroup, True, None):
                            each.bullet.life = False
                    else:    
                        if pygame.sprite.spritecollide(each.bullet, bgMap.ironGroup, False, None):
                            each.bullet.life = False
             
        # 最后画食物/道具，以及各种游戏道具效果的实现功能
        if prop.life:
            screen.blit(prop.image, prop.rect)

            # 我方坦克1碰撞 食物/道具
            if pygame.sprite.collide_rect(myTank_T1, prop):

                if prop.kind == 1:  # 敌人全毁
                    for each in allEnemyGroup:
                        if pygame.sprite.spritecollide(each, allEnemyGroup, True, None):
                            bang_sound.play()
                            enemyNumber -= 1
                    prop.life = False
                if prop.kind == 2:  # 敌人静止
                    enemyCouldMove = False
                    prop.life = False
                if prop.kind == 3:  # 子弹增强
                    myTank_T1.bullet.strong = True
                    prop.life = False
                if prop.kind == 4:  # 家得到保护
                    for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]:
                        bgMap.iron = wall.Iron()
                        bgMap.iron.rect.left, bgMap.iron.rect.top = 3 + x * 24, 3 + y * 24
                        bgMap.ironGroup.add(bgMap.iron)                
                    prop.life = False
                if prop.kind == 5:  # 坦克无敌
                    prop.life = False
                    pass
                if prop.kind == 6:  # 坦克升级
                    myTank_T1.levelUp()
                    prop.life = False
                if prop.kind == 7:  # 坦克生命+1
                    myTank_T1.life += 1
                    prop.life = False


            # 我方坦克2碰撞 食物/道具
            if pygame.sprite.collide_rect(myTank_T2, prop):
                if prop.kind == 1:  # 敌人全毁
                    for each in allEnemyGroup:
                        if pygame.sprite.spritecollide(each, allEnemyGroup, True, None):
                            bang_sound.play()
                            enemyNumber -= 1
                    prop.life = False
                if prop.kind == 2:  # 敌人静止
                    enemyCouldMove = False
                    prop.life = False
                if prop.kind == 3:  # 子弹增强
                    myTank_T2.bullet.strong = True
                    prop.life = False
                if prop.kind == 4:  # 家得到保护
                    for x, y in [(11, 23), (12, 23), (13, 23), (14, 23), (11, 24), (14, 24), (11, 25),(14, 25)]:
                        bgMap.iron = wall.Iron()
                        bgMap.iron.rect.left, bgMap.iron.rect.top = 3 + x * 24, 3 + y * 24
                        bgMap.ironGroup.add(bgMap.iron)
                    prop.life = False
                if prop.kind == 5:  # 坦克无敌
                    prop.life = False
                    pass
                if prop.kind == 6:  # 坦克升级
                    myTank_T2.levelUp()
                    prop.life = False
                if prop.kind == 7:  # 坦克生命+1
                    myTank_T2.life += 1
                    prop.life = False
                     
        # 延迟
        delay -= 1
        if not delay:
            delay = 100    
        
        pygame.display.flip()
        clock.tick(60)
    
"""
游戏目前存在的问题
地方子弹击中我方堡垒游戏没有结束
没有设置我方坦克的血量
敌方坦克血量未设置好
"""
if __name__ == "__main__":
    print("玩家一操作：W、S、A、D键，分别执行相应的移动操作，G发射子弹(英文状态下)")
    print("玩家二操作：上、下、左、右键，分别执行相应的移动操作，J发射子弹(英文状态下)")
    try:
        main()
    except SystemExit:
        pass
    except:
        traceback.print_exc()
        pygame.quit()
        input()